<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<script type="text/javascript">

// name space for this page
var p00_svcpkg_add = {
	mid: "${menuId}"
};
var svcitempath = crm.global.basePath;

// for new item model
Ext.define('p00_svcpkg_add_itemModel',{
	extend:'Ext.data.Model',
	fields: [
        {name: 'svc_item_id',  type: 'string'},
        {name: 'name',   type: 'string'},
        {name: 'price_small', type: 'number'},
        {name: 'price_middle', type: 'number'},
        {name: 'price_big', type: 'number'},
        {name: 'price_high', type: 'number'},
        {name: 'description', type: 'string'},
        {name: 'status', type: 'string'},
        {name: 'status_disp_nm', type: 'string'}
    ]
});

// new svcitem model
var p00_svcpkg_add_item = Ext.create('p00_svcpkg_add_itemModel',{
});

//store for show
p00_svcpkg_add.store = Ext.create("Ext.data.Store", {
	model: p00_svcpkg_add_itemModel,
	proxy: {
	    type: "ajax",
	    url: "svcitem/query.shtm?status=1",
	    actionMethods: {
	        read: "POST"
	    },
	    reader: {
	        root: "svcitems",
	        totalProperty: "totalCount"
	    },
	    simpleSortMode: true,
	    listeners: {
	        exception: function(proxy, resp, op, opts) {
	            var obj = Ext.JSON.decode(resp.responseText);
	            Ext.Msg.alert("信息提示", obj.errorMsgs);
	        }
	    }
	}
});

//store for add page list
p00_svcpkg_add.addpagestore = Ext.create("Ext.data.Store", {
	model: p00_svcpkg_add_itemModel,
	proxy: {
	    type: "ajax",
	    url: "svcitem/query.shtm?status=1",
	    actionMethods: {
	        read: "POST"
	    },
	    reader: {
	        root: "svcitems",
	        totalProperty: "totalCount"
	    },
	    simpleSortMode: true,
	    listeners: {
	        exception: function(proxy, resp, op, opts) {
	            var obj = Ext.JSON.decode(resp.responseText);
	            Ext.Msg.alert("信息提示", obj.errorMsgs);
	        }
	    }
	}
});

// add Panel
p00_svcpkg_add.addPanel = Ext.create("Ext.form.Panel", {
    region: "north",
    buttonAlign: "left",
    frame: true,
    bodyPadding: 5,
    items: [{
        xtype: "fieldset",
        title: "套餐信息",
        layout: "anchor",
        defaults: {
            anchor: "100%",
            layout: "hbox",
            defaultType: "textfield",
            padding: "2 0",
            defaults: {
                labelAlign: "left",
                labelWidth: 100,
                width: 240,
                labelStyle: "padding-left: 15px"
            }
        },
        items: [{
            xtype: "container",
            items: [{
                id: "p00_svcpkg_add_name",
                fieldLabel: "套餐名称",
                maxLength: 30,
                name: "name",
                allowBlank: false
            },{
                fieldLabel: "套餐编号",
                maxLength: 5,
                name: "spid",
                allowBlank: false
            },{
                xtype: "uXcombo",
                name: "status",
                comboOpts: {
                    comboId: "CD0007",
                    hasBlank: true
                },
                fieldLabel: "状态",
                labelWidth: 100,
                width: 240,
                value: "0",
                triggerAction: "all",
                forceSelection: true,
                editable: false,
                allowBlank: false
            }]
        }, {
            xtype: "container",
            items: [{
            	xtype: 'numberfield',
            	id:'p00_svcpkg_price_small',
            	maxValue: 999999999999,
            	minValue: 0,
			    decimalPrecision: 2,
                fieldLabel: "小型车费用",
                name: "price_small",
                allowBlank: false
            },{
            	xtype: 'numberfield',
            	id:'p00_svcpkg_price_middle',
            	maxValue: 999999999999,
            	minValue: 0,
			    decimalPrecision: 2,
                fieldLabel: "中型车费用",
                name: "price_middle",
                allowBlank: false
            }]
        },{
        	 xtype: "container",
             items: [{
            	xtype: 'numberfield',
            	id:'p00_svcpkg_price_big',
            	maxValue: 999999999999,
            	minValue: 0,
			    decimalPrecision: 2,
                fieldLabel: "大型车费用",
                name: "price_big",
                allowBlank: false
            },{
            	xtype: 'numberfield',
            	id:'p00_svcpkg_price_high',
            	maxValue: 999999999999,
            	minValue: 0,
			    decimalPrecision: 2,
                fieldLabel: "高级车费用",
                name: "price_high",
                allowBlank: false
            }]
        },{
        	 xtype: "container",
             items: [{
            	xtype: 'textareafield',
                fieldLabel: "描述",
                width: 485,
                maxLength: 400,
                name: "description"
            }]
        }]
    },{
        buttonAlign: "center",
        activeTab: 0,
        layout: "anchor",
        margin: "3 0",
        defaults: {
            frame: true,
            bodyStyle: "padding:5px",
            autoScroll: true,
            defaults:{
                labelAlign: "left",
                labelWidth: 100,
                width: 280,
            }
        },
        items: [{
            title: "服务项",
           	xtype: 'fieldset',
           	defaults: {
    			xtype: "button",
    			style: "margin-right: 5px; padding: 2px 20px"
    		},
           	items: [{
           		text: '添加菜单项',
           		width:120,
           		handler: p00_svcpkg_showItemsWin
           	},{
            	text: "重置",
            	handler: function() {
                    this.up("form").getForm().reset();
                    p00_svcpkg_add.addpagestore.removeAll();
                    Ext.getCmp("p00_svcpkg_add_mobile").focus();
                }
            }, {
                text: "保存",
                handler: function() {
                	var form = this.up("form").getForm();
                    form.submit({
                        url: "svcpkg/add_svcpkg.shtm?module=add&svc_item_ids=" + p00_svcpkg_getItemIds(),
                        success: function(form, action) {
                            if (action.result.success == "true") {
                            	p00_svcpkg_add.addpagestore.removeAll();
                            	form.reset();
                          		Ext.Msg.alert("信息提示", "保存成功");
                            	Ext.getCmp("p00_svcpkg_add_mobile").focus();
                            }
                        },
                        faliure: function(form, action) {
                            Ext.Msg.alert("提示信息", action.result.errorMsgs);
                        }
                    });
                }
            }]
        }]
    }]
});

// 获取服务项列表所有id
function p00_svcpkg_getItemIds(){
	var ids = "";
	p00_svcpkg_add.addpagestore.each(function(record){
		ids += record.get('svc_item_id') + ',';
	});
	if(ids.length > 0){
		ids = ids.substring(0, ids.length-1);
	}
	return ids;
}

// selected grid 
p00_svcpkg_add.gridPanel = Ext.create("Ext.grid.Panel", {
	region: "center",
	frame: true,
	margin:  0,
	autoScroll: true,
    store: p00_svcpkg_add.addpagestore,
    features: [{
    	ftype: 'summary'
    }],
    columns: [
    	{ text: "编号", width: 100, dataIndex: "svc_item_id",renderer:p00_svcpkg_itemId},
        { text: "名称", width: 100, dataIndex: "name", 
        		summaryType:'count', summaryRenderer: function(value){
        			return '总数：' + value;}},
        { text: "价格（小型车）", width: 200, dataIndex: "price_small", 
        		summaryType:'sum', summaryRenderer: function(value){
        			Ext.getCmp('p00_svcpkg_price_small').setValue(value);
        			return '推荐价格：<span>' + value + '</span>';}},
        { text: "价格（中型车）", width: 200, dataIndex: "price_middle", 
        		summaryType:'sum', summaryRenderer: function(value){
        			Ext.getCmp('p00_svcpkg_price_middle').setValue(value);
        			return '推荐价格：' + value;}},
        { text: "价格（大型车）", width: 200, dataIndex: "price_big", 
        		summaryType:'sum', summaryRenderer: function(value){
        			Ext.getCmp('p00_svcpkg_price_big').setValue(value);
        			return '推荐价格：' + value;}},
        { text: "价格（高级车）", width: 200, dataIndex: "price_high", 
        		summaryType:'sum', summaryRenderer: function(value){
        			Ext.getCmp('p00_svcpkg_price_high').setValue(value);
        			return '推荐价格：' + value;}},
    ]
});

function p00_svcpkg_itemId(value) {
	return value = '<span class="p00_svcpkg_itemId">' + value + '</span>';
}

//  show svcitem list for select 
function p00_svcpkg_showItemsWin() {
	var titleName = '选择服务项';
	var win;
	//debugger;
    if (!win) {
		    Ext.ClassManager.setAlias('Ext.selection.CheckboxModel', 'selection.checkboxmodel');
    		
    		p00_svcpkg_add.store.load();
    		// grid panel
			gridPanel = Ext.create("Ext.grid.Panel", {
			region: "center",
			frame: true,
			margin:  0,
			autoScroll: true,
		    store: p00_svcpkg_add.store,
		    multiSelect: true,
		    selModel: {
		    	selType: 'checkboxmodel'
		    },
		    columns: [
		        { text: "编号",  width: 80,  dataIndex: "svc_item_id" },
		        { text: "名称", width: 100, dataIndex: "name"},
		        { text: "价格（小型车）", width: 100, dataIndex: "price_small"},
		        { text: "价格（中型车）", width: 100, dataIndex: "price_middle"},
		        { text: "价格（大型车）", width: 100, dataIndex: "price_big"},
		        { text: "价格（高级车）", width: 100, dataIndex: "price_high"},
		    ],
		    viewConfig: {
		    	forceFit: false
		    },
		    tbar: [{
		    	text: '确定',
		    	handler: function(){
		    		var returnvalue = '';
		    		var rows =  gridPanel.getSelectionModel().getSelection();
		    		p00_svcpkg_add.addpagestore.loadData(rows);
		    		win.hide();
		    	}
		    }]
		    
		});
        win = Ext.widget('window', {
            title: titleName,
            closeAction: 'hide',
            padding: 5,
            width: 650,
            minWidth:650,
            height: 650,
            minHeight: 650,
            layout: 'fit',
            resizable: true,
            modal: true,
            items: gridPanel
        });
    }
    win.show();
}

//add to pTab
Ext.getCmp(p00_svcpkg_add.mid).add({
    xtype: "panel",
    bodyPadding: 0,
    border: 0,
    layout: "border",
    margin: 0,
    items: [p00_svcpkg_add.addPanel, p00_svcpkg_add.gridPanel]
});

// update svcpkg total value
function p00_svcpkg_update_condition(price_small, price_middle, price_big, price_high){
	p00_svcpkg_add_item.set('price_small', price_small);
	p00_svcpkg_add_item.set('price_middle', price_middle);
	p00_svcpkg_add_item.set('price_big', price_big);
	p00_svcpkg_add_item.set('price_high', price_high);
	p00_svcpkg_add.addPanel.loadRecord(p00_svcpkg_add_item);
}

Ext.getCmp("p00_svcpkg_add_name").focus();
</script>

